/*
 * BlockFile.cpp
 *
 */

#include "BlockFile.h"
#include "../log/Logger.h"
#include "../utils/Utils.h"

BlockFile::BlockFile() {
}

BlockFile::~BlockFile() {
}

bool BlockFile::get(BigInt number, BlockPersistor& persistor) {
	getDescriptor().seekg(number*persistor.size(), std::fstream::beg);
	Logger::getInstance().debug("BlockFile: se leyo el bloque numero: " + Utils::toString(number));
	return persistor.load(getDescriptor());
}

bool BlockFile::append(BlockPersistor& persistor) {
	getDescriptor().seekp(0, std::fstream::end);
	if (persistor.save(getDescriptor())) {
		return true;
	}
	return false;
}

bool BlockFile::write(BigInt number, BlockPersistor& persistor) {
	getDescriptor().seekp(number*persistor.size(), std::fstream::beg);
	if (persistor.save(getDescriptor())) {
		Logger::getInstance().debug("BlockFile: se escribio el bloque numero: " + Utils::toString(number));
		return true;
	}
	return false;
}

